﻿package pluginfw.nio.filters
{
    import com.sh.game.consts.*;
    import com.sh.game.exception.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.net.*;
    import flash.utils.*;
    import pluginfw.nio.handler.*;
    import pluginfw.nio.imsg.*;
    import pluginfw.nio.message.*;
    import pluginfw.proxy.*;

    public class SimpleFilter extends BaseFilter
    {
        protected var handlers:Dictionary;
        protected var proxyMgr:ProxyClient;
        private var mark:int;
        private var size:int;
        private var type:int;
        private var messageid:int;
        private var crc:int;
        private var group:int;
        private var cmd:int;
        private var data:ByteArray;
        private var end:int;
        private var handler:IGroupHandler;
        protected var records:Dictionary;
        protected var totalbytes:int = 0;
        protected var lasttime:int = 0;

        public function SimpleFilter()
        {
            this.handlers = new Dictionary();
            this.records = new Dictionary();
            return;
        }// end function

        public function addHandler(param1:int, param2:IGroupHandler) : void
        {
            this.handlers[param1] = param2;
            return;
        }// end function

        public function setProxyManager(param1:ProxyClient) : void
        {
            this.proxyMgr = param1;
            return;
        }// end function

        override public function handleRead(param1:Socket) : void
        {
            var message:SimpleMessage;
            var message2:SimpleMessage;
            var buffermessage:BufferMessage;
            var buffermessage2:BufferMessage;
            var newbytes:ByteArray;
            var socket:* = param1;
            if (socket.bytesAvailable)
            {
                socket.readBytes(buffer, buffer.length, socket.bytesAvailable);
                while (buffer.bytesAvailable > 4)
                {
                    
                    this.size = buffer.readInt();
                    if (buffer.bytesAvailable < this.size)
                    {
                        buffer.position = buffer.position - 4;
                        return;
                    }
                    this.type = buffer.readByte();
                    if (this.type == 0)
                    {
                        this.messageid = buffer.readUnsignedShort();
                        message = new SimpleMessage();
                        this.crc = buffer.readUnsignedShort();
                        message.prase(this.size, this.messageid, this.crc, buffer);
                        this.handler = this.handlers[message.getGroup()];
                        if (this.handler)
                        {
                            try
                            {
                                this.handler.dealSimpleMessage(message);
                            }
                            catch (error:Error)
                            {
                                if (Config.debug)
                                {
                                    throw error;
                                }
                                else if (Config.Log > LogType.ERROR)
                                {
                                    NbLog.getInstance().checkError(error, ErrorType.SysMsgError, "group:" + message.getGroup() + " cmd:" + message.getCmd() + "  ");
                                }
                            }
                        }
                        continue;
                    }
                    if (this.type == 7)
                    {
                        this.messageid = buffer.readUnsignedShort();
                        message2 = new SimpleMessage(true);
                        this.crc = buffer.readUnsignedShort();
                        message2.prase(this.size, this.messageid, this.crc, buffer);
                        this.handler = this.handlers[message2.getGroup()];
                        if (this.handler)
                        {
                            try
                            {
                                this.handler.dealSimpleMessage(message2);
                            }
                            catch (error:Error)
                            {
                                if (Config.debug)
                                {
                                    throw error;
                                }
                                else if (Config.Log > LogType.ERROR)
                                {
                                    NbLog.getInstance().checkError(error, ErrorType.SysMsgError, "group:" + message2.getGroup() + " cmd:" + message2.getCmd() + "  ");
                                }
                            }
                        }
                        continue;
                    }
                    if (this.type == 3)
                    {
                        this.messageid = buffer.readUnsignedShort();
                        buffermessage = new BufferMessage();
                        this.crc = buffer.readUnsignedShort();
                        buffermessage.parse(this.size, this.messageid, this.crc, buffer);
                        this.handler = this.handlers[buffermessage.getGroup()];
                        if (this.handler)
                        {
                            try
                            {
                                this.handler.dealBufferMessage(buffermessage);
                            }
                            catch (error:Error)
                            {
                                if (Config.debug)
                                {
                                    throw error;
                                }
                                else if (Config.Log > LogType.ERROR)
                                {
                                    NbLog.getInstance().checkError(error, ErrorType.SysMsgError, "group:" + buffermessage.getGroup() + " cmd:" + buffermessage.getCmd() + "  ");
                                }
                            }
                        }
                        continue;
                    }
                    if (this.type == 8)
                    {
                        this.messageid = buffer.readUnsignedShort();
                        buffermessage2 = new BufferMessage(true);
                        this.crc = buffer.readUnsignedShort();
                        buffermessage2.parse(this.size, this.messageid, this.crc, buffer);
                        this.handler = this.handlers[buffermessage2.getGroup()];
                        if (this.handler)
                        {
                            try
                            {
                                this.handler.dealBufferMessage(buffermessage2);
                            }
                            catch (error:Error)
                            {
                                if (Config.debug)
                                {
                                    throw error;
                                }
                                else if (Config.Log > LogType.ERROR)
                                {
                                    NbLog.getInstance().checkError(error, ErrorType.SysMsgError, "group:" + buffermessage2.getGroup() + " cmd:" + buffermessage2.getCmd() + "  ");
                                }
                            }
                        }
                        continue;
                    }
                    if (this.type == 5)
                    {
                        newbytes = new ByteArray();
                        buffer.position = 5;
                        buffer.readBytes(newbytes, 0, (this.size - 1));
                        newbytes.position = 0;
                        newbytes.uncompress();
                        newbytes.position = 0;
                        buffer.readBytes(newbytes, newbytes.length, buffer.bytesAvailable);
                        buffer = newbytes;
                    }
                }
                buffer.clear();
            }
            return;
        }// end function

        override public function handleWrite(param1:IMessage, param2:Socket) : void
        {
            if (param2 != null && param2.connected)
            {
                param2.writeBytes(param1.getBuffer());
                param2.flush();
            }
            return;
        }// end function

        override public function handleConnect(param1:Socket) : void
        {
            return;
        }// end function

        override public function handleDisconnect(param1:Socket) : void
        {
            param1.close();
            return;
        }// end function

    }
}
